package com.jlkf.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jlkf.dao.TblCourseCollectionMapper;
import com.jlkf.dao.TblCourseLikeMapper;
import com.jlkf.dao.TblCourseMapper;
import com.jlkf.dao.TblLessonMapper;
import com.jlkf.dao.TblMessageMapper;
import com.jlkf.dao.TblTeacherMapper;
import com.jlkf.pojo.TblCourse;
import com.jlkf.pojo.TblCourseCollection;
import com.jlkf.pojo.TblCourseLike;
import com.jlkf.pojo.TblLesson;
import com.jlkf.pojo.TblMessage;
import com.jlkf.pojo.TblTeacher;
import com.jlkf.service.TblCourseLikeService;
import com.jlkf.view.CourseLikeListView;
import com.platform.server.response.response.pojo.ReturnData;
import com.platform.server.util.StringTools;

@Transactional
@Service
public class TblCourseLikeServiceImpl implements TblCourseLikeService {

	// 用户收藏课程信息
	@Autowired
	private TblCourseLikeMapper tblCourseLikeMapper;
	
	@Autowired
	private TblCourseMapper tblCourseMapper;
	
	@Autowired
	private TblMessageMapper tblMessageMapper;
	
	@Autowired
	private TblTeacherMapper tblTeacherMapper;
	
	@Autowired
	private TblCourseCollectionMapper tblCourseCollectionMapper;
	
	@Autowired
	private TblLessonMapper tblLessionMapper;
	
	@Override
	public ReturnData likeCourse(Long courseId, Long userId) {
	
		 if(courseId==null){
			 return new ReturnData(1001, "课程不能为空");
		 }
		 if(userId==null){
			 return new ReturnData(1002, "用户信息不能为空");
		 }
		TblCourseLike record = new TblCourseLike();
		record.setgUserId(userId);
		record.setgCreatetime(new Date());
		record.setgCourseId(courseId);
		record.setgVersion(1);
		int i = tblCourseLikeMapper.insertSelective(record );
		  if(i==1){
			  return  new ReturnData(200, "点赞成功");
		  }
		  return  new ReturnData(2001, "点赞失败");
	}


	@Override
	public ReturnData cancelCourseLike(Long courseId, Long userId) {
	       if(courseId!=null && userId != null){
	    	   Map<String, Object> map = new HashMap<String, Object>();
	    	   map.put("userId", userId);
	    	   map.put("courseId", courseId);
	    	   int d = tblCourseLikeMapper.deleteByPrimaryCourse(map);
	    	     if(d==1){
	    	    	 return new ReturnData(200, "取消点赞成功");
	    	     }
	       }
	       return new ReturnData(2001, "取消点赞失败");
	}


	@Override
	public ReturnData courseLikeList(Long userId,Integer pageSize, Integer pageNumber) {
		if(pageNumber==null){
			pageNumber=1;
		}
		if(pageSize==null){
			pageSize=10;
		}
		PageHelper.startPage(pageNumber, pageSize);
		
	    List<TblCourseCollection> tblCourseCollections = tblCourseCollectionMapper.selectList(userId);
	    List<CourseLikeListView> list = new ArrayList<>();
	    if(tblCourseCollections.size()>0){
		      for (TblCourseCollection tbl : tblCourseCollections) {
				  Long getgCourseId = tbl.getgCourseId();
				  TblCourse tblCourse = tblCourseMapper.selectByPrimaryKey(getgCourseId);
				  String teacher = null;
				  if(tblCourse!=null){
					  if(!StringTools.isEmpty(tblCourse.getgRelatedCourse())){
						   String[] ids = tblCourse.getgRelatedCourse().split(",");
						   for(int i = 0;i<ids.length;i++){
							   TblLesson lesson = tblLessionMapper.selectByPrimaryKey(Long.valueOf(ids[i]));
							   if(lesson!=null){
								    teacher = lesson.getTeacherName();
								    break;
							   }
						   }
					  }
					  CourseLikeListView courseview = new CourseLikeListView();
					  courseview.setCourseImg(tblCourse.getgCourseImg());
					  courseview.setCourseName(tblCourse.getgCourseName());
					  courseview.setLikeTime(tbl.getgCreatetime());
					  courseview.setPirce(tblCourse.getgCourseMoney());
					  courseview.setTeacherName(teacher);
					  courseview.setId(tbl.getgId());
					  courseview.setCourseId(getgCourseId);
					  list.add(courseview);	  
				  }
			}
	    }
	    PageInfo<TblCourseCollection> info = new PageInfo<>(tblCourseCollections);
	    
		return new ReturnData(list, (int)info.getPages(), (int)info.getTotal(), 200, "收藏列表");
	}


	@Override
	public ReturnData coursePraise(Long userId, Long courseId,Integer type,Long id) {
		if(type==1){
			TblCourseCollection record = new TblCourseCollection();
			record.setgCreatetime(new Date());
			record.setgUserId(userId);
			record.setgCourseId(courseId);
			record.setgVersion(1);
			int i = tblCourseCollectionMapper.insertSelective(record);
			if(i==1){
				return new ReturnData(200,"收藏成功");
			}else{
				return new ReturnData(2001,"收藏失败");
			}
		}else{
			int d = tblCourseCollectionMapper.deleteByPrimaryCourse(userId, courseId);
               if(d==1){
            	   return new ReturnData( 200, "取消收藏成功");
               }			
		}
		return new ReturnData( 2001, "取消收藏失败");
	}
}
